package com.adkfp.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class PowerNodes {
    private Connection conn ;
    
    private String dbdriver ;
    private String dbhost;
    private String dbuser;
    private String dbpassword;

    public PowerNodes() throws IOException, ClassNotFoundException, SQLException  {
    	Properties properties = new Properties();
    	// 使用ClassLoader加载properties配置文件生成对应的输入流
    	//InputStream in = Reader.class.getResourceAsStream("/database.properties"); 
    	InputStream in = Reader.class.getResourceAsStream("/database.properties");
    	// 使用properties对象加载输入流
        properties.load(in);
    	//获取key对应的value值
		
    	Class.forName(properties.getProperty("jdbc.driverClass"));//"com.mysql.jdbc.Driver");
		dbhost =properties.getProperty("jdbc.jdbcUrl") ;//"jdbc:mysql://192.168.0.198:3306/qudao";
		dbuser = properties.getProperty("jdbc.user","root");
		dbpassword = properties.getProperty("jdbc.password", "123456");
		conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
    }  

	public PowerNodes(String dbdriver, String dbhost, String dbuser, String dbpassword) throws ClassNotFoundException, SQLException {
		super();
		this.dbdriver = dbdriver;
		this.dbhost = dbhost;
		this.dbuser = dbuser;
		this.dbpassword = dbpassword;
    	Class.forName(this.dbdriver);//"com.mysql.jdbc.Driver");
    	conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
	}

	public void ConnectDB() throws SQLException{
    	conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
    }
	
	protected void finalize() throws SQLException{
    	conn.close();
    }
	
	public JSONArray getResult() throws ClassNotFoundException, SQLException, IOException {
 		Statement state = conn.createStatement();
		String SQLStr = " select distinct * from adk_powergroup" ;
		ResultSet rs = state.executeQuery(SQLStr);
 
        //List<PowerNodes> progNodes= new ArrayList<PowerNodes>();
		JSONArray rtJA = new JSONArray();
        while(rs.next()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",rs.getString("GroupID"));
            jsonObject.put("title",rs.getString("GroupName"));
            jsonObject.put("checkArr","0");
            //jsonObject.put("children", getChild(rs.getString("CtrlAddr")));
            rtJA.add(jsonObject);
        }
        state.close();
        
        System.out.print(rtJA.toString());
        return rtJA;
    }
	
/*    public JSONArray getChild(String aCtrlAddr) throws ClassNotFoundException, SQLException, IOException{

        Statement state = conn.createStatement();
        String SQLStr = " select distinct * from ADK_Door where CtrlAddr ="+aCtrlAddr;
        ResultSet rs = state.executeQuery(SQLStr);
        
        //List<DeptNodes> progNodes= new ArrayList<DeptNodes>();
        
		JSONArray rtJA = new JSONArray();
        while(rs.next()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",rs.getString("AutoID"));
            jsonObject.put("title",rs.getString("Name"));
            rtJA.add(jsonObject);
        }
        state.close();
        return rtJA;   
    }*/
}
